package com.mini.gateway.web.domain;

import java.io.Serializable;

/**
 * API返回结果封装
 * @since 2018/1/4 0004
 */
public class JsonResult<T> implements Serializable {

    protected String code;
    protected String msg;
    protected T data;
    protected boolean success;

    public JsonResult() {}

    public static <T> JsonResult success(){
        JsonResult<T> result = new JsonResult<>();
        result.success = true;
        result.code = ResultConstants.NORMAL.getCode();
        result.msg = ResultConstants.NORMAL.getMsg();
        return result;
    }

    public static <T> JsonResult success(T data){
        JsonResult<T> result = new JsonResult<>();
        result.success = true;
        result.code = ResultConstants.NORMAL.getCode();
        result.msg = ResultConstants.NORMAL.getMsg();
        result.data = data;
        return result;
    }

    public static <T> JsonResult error(){
        JsonResult<T> result = new JsonResult<>();
        result.success = false;
        result.code = ResultConstants.ERROR.getCode();
        result.msg = ResultConstants.ERROR.getMsg();
        return result;
    }

    public static <T> JsonResult error(String msg){
        JsonResult<T> result = new JsonResult<>();
        result.success = false;
        result.code = ResultConstants.ERROR.getCode();
        result.msg = msg;
        return result;
    }

    public static <T> JsonResult error(String code, String msg){
        JsonResult<T> result = new JsonResult<>();
        result.success = false;
        result.code = code;
        result.msg = msg;
        return result;
    }

    public static <T> JsonResult error(String msg, T data){
        JsonResult<T> result = new JsonResult<>();
        result.success = false;
        result.code = ResultConstants.ERROR.getCode();
        result.msg = msg;
        result.data = data;
        return result;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
}
